package com.zoe.onelink.business.ws;

import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.common.util.Compiler;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
import org.apache.cxf.helpers.JavaUtils;

import javax.xml.namespace.QName;
import java.io.File;
import java.util.List;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2023</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2023-02-14
 */
public class OnelinkDynamicClientFactory extends DynamicClientFactory {

    private static final String APP_CLASS_LOADER = "jdk.internal.loader.ClassLoaders$AppClassLoader";

    protected OnelinkDynamicClientFactory(Bus bus) {
        super(bus);
    }

    @Override
    public Client createClient(String wsdlUrl, QName service,
                               ClassLoader classLoader, QName port,
                               List<String> bindingFiles) {
        return super.createClient(wsdlUrl, service, classLoader, port, bindingFiles);
    }

    @Override
    protected boolean compileJavaSrc(String classPath, List<File> srcList, String dest) {
        Compiler compiler;
        if (APP_CLASS_LOADER.equals(Thread.currentThread().getContextClassLoader().getClass().getName())) {
            compiler = new org.apache.cxf.common.util.Compiler();
        } else {
            compiler = new OnelinkCxfCompile();
        }
        compiler.setClassPath(classPath);
        compiler.setOutputDir(dest);
        // JDK9 - JDK11
        if (JavaUtils.isJava9Compatible()) {
            compiler.setTarget("9");
        } else {
            compiler.setTarget("1.8");
        }
        return compiler.compileFiles(srcList);
    }

    public static OnelinkDynamicClientFactory newInstance() {
        Bus bus = BusFactory.getThreadDefaultBus();
        return new OnelinkDynamicClientFactory(bus);
    }

}
